﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.ServiceModel;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Logging;
using Microsoft.Practices.Unity;
using Utility;
using Interfaces;
using Services;
namespace Models
{
    public class NewsModel : BindableBase, INewsModel
    {
        public INewsResults Results { get { return _results; } private set { SetProperty(ref _results, value); } }
        public int ResultCount { get { return _resultCount; } private set { SetProperty(ref _resultCount, value); } }
        public string Keyword { get { return _keyword; } private set { SetProperty(ref _keyword, value); } }

        private readonly ILoggerFacade _logger;
        private readonly IUnityContainer _container;
        private INewsResults _results;
        private int _resultCount = 30;
        private string _keyword = "IBM";

        public NewsModel(ILoggerFacade logger, IUnityContainer container)
        {
            _logger = logger;
            _container = container;
            GetService(_keyword);
        }

        public void GetService(string keyword)
        {
            try
            {
                Keyword = keyword;

                var factory = new ChannelFactory<INewsResultsService>(new NetNamedPipeBinding(), "net.pipe://localhost/NewsResultsService");
                var _service = factory.CreateChannel();

                ICommunicationObject comobj = (ICommunicationObject)_service;
                if (comobj.State != CommunicationState.Faulted)
                {
                    Results = _service.Get(_keyword, _resultCount);
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}
